package com.hust.hui.quick.service.repository;

import com.hust.hui.quick.service.common.ArticleStateEnum;
import com.hust.hui.quick.service.common.ArticleUtil;
import com.hust.hui.quick.service.dao.ArticleMapper;
import com.hust.hui.quick.service.domain.ArticleEntity;
import com.hust.hui.quick.service.exception.IllegalArticleException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by yihui on 2017/7/19.
 */
@Repository
public class ArticleRepository {

    @Autowired
    private ArticleMapper articleMapper;


    public void init() {
        articleMapper.create();
    }


    public ArticleEntity addArticle(ArticleEntity articleEntity) {
        checkTitle(articleEntity.getTitle());

        checkDesc(articleEntity.getDesc());


        if (articleEntity.getState() == ArticleStateEnum.PUBLISHED.getState()) {
            articleEntity.setPublishTime((int) (System.currentTimeMillis() / 1000));
            articleEntity.setSort(ArticleUtil.getSort(articleEntity.getPublishTime()));
        }


        if (articleEntity.getTags() == null) {
            articleEntity.setTags("");
        }


        if (articleEntity.getContent() == null) {
            articleEntity.setContent("");
        }


        int ans = articleMapper.insert(articleEntity);
        return ans > 0 ? articleEntity : null;
    }


    /**
     * 置顶操作
     *
     * @param id
     * @param toUp true 表示置顶； false 表示取消
     * @return
     */
    public ArticleEntity toUp(long id, boolean toUp) {
        ArticleEntity articleEntity = checkArticle(id);

        if (articleEntity.getState() != ArticleStateEnum.PUBLISHED.getState()) {
            throw new IllegalArticleException("未发表的博客不支持置顶!");
        }


        boolean isUpped = ArticleUtil.isUpped(articleEntity.getSort());
        if (isUpped & toUp) {
            return articleEntity;
        }


        if (toUp) { // 置顶
            articleEntity.setSort(ArticleUtil.getSort(articleEntity.getPublishTime()));
        } else { // 取消置顶
            articleEntity.setSort(articleEntity.getPublishTime());
        }


        articleMapper.update(articleEntity);
        return articleEntity;
    }


    /**
     * 发表草稿箱内的博文
     *
     * @param id
     */
    public ArticleEntity publish(long id) {
        ArticleEntity articleEntity = checkArticle(id);

        if (articleEntity.getState() == ArticleStateEnum.PUBLISHED.getState()) {
            throw new IllegalArticleException("博文已发表!");
        }


        articleEntity.setState(ArticleStateEnum.PUBLISHED.getState());
        articleEntity.setPublishTime((int) (System.currentTimeMillis() / 1000));
        articleEntity.setSort(articleEntity.getPublishTime());


        articleMapper.update(articleEntity);
        return articleEntity;
    }



    /**
     * 更新， 标题 + 描述 + 内容 + 是否置顶 + 是否发布 + 标签 + 目录
     *
     * @return
     */
    public ArticleEntity updateArticle(ArticleEntity articleEntity) {
        if (articleEntity.getId() <= 0) {
            throw new IllegalArticleException("请指定更新的博客id!");
        }


        if (articleMapper.update(articleEntity) > 0) {
            return articleMapper.queryById(articleEntity.getId());
        }

        return null;
    }


    private void checkTitle(String title) {
        if (title == null || title.length() > 40) {
            throw new IllegalArticleException("标题长度应在 0 - 40之间");
        }
    }


    private void checkDesc(String desc) {
        if (desc != null && desc.length() > 500) {
            throw new IllegalArticleException("博客简述请勿超过500字符");
        }
    }


    private ArticleEntity checkArticle(long id) {
        ArticleEntity articleEntity = articleMapper.queryById(id);
        if (articleEntity == null) {
            throw new IllegalArticleException("博客不存在!");
        }

        return articleEntity;
    }

}
